package logic

import (
	"context"
	"fmt"
	"math/rand"
	"nove/models/cache"
	"time"

	"nove/noverpc/internal/svc"
	"nove/noverpc/noverpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *noverpc.SendSmsRequest) (*noverpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	res, _ := cache.RDB.Get(l.ctx, "sendSmsTime"+in.Account).Int64()
	if res >= 1 {
		return nil, fmt.Errorf("一分钟内只能发送一次")
	}
	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Account, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if sms.Code != 2 {
	//	return nil, fmt.Errorf(sms.Msg)
	//}
	cache.RDB.Set(l.ctx, "sendSms"+in.Account, code, time.Minute*5)

	cache.RDB.Incr(l.ctx, "sendSmsTime"+in.Account)
	cache.RDB.Expire(l.ctx, "sendSmsTime"+in.Account, time.Minute*1)
	return &noverpc.SendSmsResponse{}, nil
}
